/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.wlhy.report.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.enums.ReportPlatformEnum;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.ImageCompressUtil;
import org.jeecg.common.util.LngLonUtil;
import org.jeecg.modules.third.digitalseal.utils.PdfEditUtil;
import org.jeecg.modules.third.jg.report.freight.information.constant.enums.AhUploadStatusEnum;
import org.jeecg.modules.third.jg.report.freight.information.constant.enums.FileCodeEnum;
import org.jeecg.modules.third.jg.report.freight.information.constant.enums.ReportDocTypeEnum;
import org.jeecg.modules.third.jg.report.freight.information.dto.AppInfoDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.DriverDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.FileDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.VehicleDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.financial.FinancialDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.shippingnote.LocationDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.shippingnote.ShippingNoteDTO;
import org.jeecg.modules.third.jg.report.freight.information.model.R;
import org.jeecg.modules.third.jg.report.freight.information.service.HuaiBeiReportPlatformService;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriverReport;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverReportService;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicleReport;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicleType;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleReportService;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.report.IReportService;
import org.jeecg.modules.wlhy.shipping.constant.ShippingPayStatus;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsPackage: org.jeecg.modules.wlhy.report.impl
 * @Author: duanlikao
 * @CreateTime: 2023-03-08 10:38
 * @Description:
 * @Version: 1.0
 */
@Service
@Slf4j
public class HuaibeiReportService implements IReportService {

    @Resource
    private BaseReportService baseReportService;

    @Resource
    private IHyDriverService driverService;

    @Resource
    private IHyVehicleService vehicleService;

    @Resource
    private IHyDriverReportService hyDriverReportService;

    @Resource
    private IHyVehicleReportService vehicleReportService;

    @Autowired
    private HuaiBeiReportPlatformService huaiBeiReportPlatformService;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private IHyShippingNoteBillService hyShippingNoteBillService;

    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    //银行文件路径
    @Value(value = "${jeecg.path.bank}")
    private String bankPath;

    @Override
    public ReportPlatformEnum getReportType() {
        return ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI;
    }

    @Override
    public void checkReport() {
        //淮北监管平台
        if(!HyCfgUtil.isUsed2("huaibei_report")){
            throw new JeecgBootException("淮北上报服务暂未开启,请联系管理。");
        }
    }

    @Override
    public AppInfoDTO getAppInfo() {
        return huaiBeiReportPlatformService.getAppInfo();
    }

    @Override
    public boolean report(HyShippingNote hyShippingNote) {
        return false;
    }

    @Override
    public Result reportShippingNote(HyShippingNote hyShippingNote) {
        Result result = this.checkDriverAndVehicle(hyShippingNote.getId());
        if (!result.getCode().equals(CommonConstant.SC_OK_200)) {
            return result;
        }

        return this.uploadHuaibeiShippingNote(hyShippingNote);
    }

    @Override
    public Result reportCapitalFlow(HyShippingNote hyShippingNote) {
        Result result = this.checkDriverAndVehicle(hyShippingNote.getId());
        if (!result.getCode().equals(CommonConstant.SC_OK_200)) {
            return result;
        }

        return this.uploadHuaibeiCapitalFlow(hyShippingNote.getId());
    }


    //上报运单和资金流水前，先确认司机、车辆的上传状态
    private Result checkDriverAndVehicle(String shippingId) {

        HyShippingNote hyShippingNote = this.hyShippingNoteService.getById(shippingId);
        if (null == hyShippingNote) {
            return Result.error("运单不存在");
        }

        String driverId = hyShippingNote.getDriverId();
        String vehicleId = hyShippingNote.getVehicleId();
        if (StrUtil.isEmpty(driverId)) {
            return Result.error("当前运单司机信息不存在");
        }

        if (StrUtil.isEmpty(vehicleId)) {
            return Result.error("当前运单车辆信息不存在");
        }

        //自动上报司机和车辆配置
        String autoUpload = HyCfgUtil.getCfgItem2("huaibei_report","auto_upload");

        //淮北监管写死公司id 1000
        boolean bDriverUploadStatus = this.hyDriverReportService.checkUploadStatus(driverId, "1000",
                ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode());
        if (!bDriverUploadStatus) {
            if (StrUtil.isEmpty(autoUpload) || !autoUpload.equals("1")) {
                return Result.error("请先同步司机数据");
            }
            log.warn("司机未上报淮北监管，开始同步司机数据, {}", driverId);
            Result dResult = this.uploadDriver(driverId);
            if (!dResult.getCode().equals(CommonConstant.SC_OK_200)) {
                return dResult;
            }
        }

        //淮北监管写死公司id 1000
        boolean bVehicleUploadStatus = this.vehicleReportService.checkUploadStatus(vehicleId, "1000",
                ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode());
        if (!bVehicleUploadStatus) {
            if (StrUtil.isEmpty(autoUpload) || !autoUpload.equals("1")) {
                return Result.error("请先同步车辆数据");
            }
            log.warn("车辆未上报淮北监管，开始同步车辆数据, {}", vehicleId);
            Result vResult = this.uploadVehicle(vehicleId);
            if (!vResult.getCode().equals(CommonConstant.SC_OK_200)) {
                return vResult;
            }
        }

        return Result.OK();
    }

    public Result uploadHuaibeiCapitalFlow(String shippingId){

        HyShippingNote hyShippingNote = hyShippingNoteService.getById(shippingId);
        if (!hyShippingNote.getShippingPayStatus().equals(ShippingPayStatus.支付完成.getValue())) {
            return Result.error("该运单未支付完成");
        }

        if ("1".equals(hyShippingNote.getTongdaUploadStatus())) {
            return Result.error("该资金流水已上报，请勿重复上报");
        }

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("shipping_id", shippingId);
        List<HyShippingNoteBill> shippingNoteBills = hyShippingNoteBillService.list(queryWrapper);
        if (CollectionUtils.isEmpty(shippingNoteBills)) {
            throw new JeecgBootException("未找到对应数据");
        }

//        List<String> bankHdImgs = new ArrayList<>();
//        for(HyShippingNoteBill hyShippingNoteBill:shippingNoteBills){
//            String bankHdImg = "1".equals(hyShippingNoteBill.getPayChannel()) ? hyShippingNoteBill.getBankHd() : hyShippingNoteBill.getBankHdImg();
//            if (StringUtils.isEmpty(bankHdImg)) {
//                throw new JeecgBootException("该运单银行回单信息不存在！");
//            }
//            String pathTmp =  "";
//            //文件名是pdf改成jpg
//            String suffix = bankHdImg.substring(bankHdImg.lastIndexOf("."));
//            if(suffix.equals(".pdf")){
//                pathTmp =  uploadpath  + File.separator + bankHdImg.substring(0, bankHdImg.lastIndexOf(".")) + ".jpg";
//                File file = new File(pathTmp);
//                if (!file.exists()) {
//                    pathTmp =  bankPath  + File.separator +  bankHdImg.substring(0, bankHdImg.lastIndexOf(".")) + ".jpg";
//                }
//            }
//            File file = new File(pathTmp);
//            if (!file.exists()) {
//                throw new JeecgBootException("该运单银行回单上传的图片不存在!");
//            }
//
//            bankHdImgs.add( pathTmp );
//        }

        FinancialDTO financialDTO = baseReportService.getFinancialDTO(shippingNoteBills,hyShippingNote);
        String jsonString = JSONObject.toJSONString(financialDTO);

        String reporDocType = ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_ZJ.getCode();
        ReportDocTypeEnum reportDocTypeEnum = ReportDocTypeEnum.getReportDocTypeEnumByVal(reporDocType);
        try {
            String reportResult = huaiBeiReportPlatformService.sendReport(reportDocTypeEnum,jsonString, false);
            if(StringUtils.isNotEmpty(reportResult)){
                R result = JSON.parseObject(reportResult, R.class);
                boolean res =  result.getCode() == 1001 && result.isSuccess() ?  true : false;
                if(res){//上传回单
                    for(HyShippingNoteBill shippingNoteBillDb : shippingNoteBills){
                        if (StrUtil.isNotEmpty(shippingNoteBillDb.getBankHd())) {
                            this.uploadFile(
                                    new FileDTO()
                                            .setCategory(FileCodeEnum.FILE_CODE_TYPE_34.type)
                                            .setShippingNoteNumber(hyShippingNote.getShippingNoteNumber())
                                            .setImageId(FileCodeEnum.FILE_CODE_TYPE_34.code)
                                            .setImageType("pdf")
                                            .setBase64Image( PdfEditUtil.fileToBase64( bankPath  + File.separator + shippingNoteBillDb.getBankHd()) )
                            );
                        }
                    }
                }
//                for(HyShippingNoteBill shippingNoteBillDb :  shippingNoteBills){
//                    shippingNoteBillDb.setUploadStatus(res ?  "1" : "2");
//                    shippingNoteBillDb.setUploadDate(new Date());
//                    shippingNoteBillDb.setReportFailureReason(res ?  "" : result.getMsg()+result.getData());
//                    shippingNoteBillDb.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode());
//                }

                //临时占用道达上报字段
                hyShippingNote.setTongdaUploadStatus(res ? "1" : "2");
                hyShippingNote.setTongdaUploadDate(new Date());
                hyShippingNote.setAlctStatus(res ? "" : result.getMsg() + result.getData());
                hyShippingNoteService.updateById(hyShippingNote);
                //hyShippingNoteBillService.updateBatchById(shippingNoteBills);
                return res ? Result.OK("上报成功") : Result.error(result.getMsg());
            }else{
                throw new JeecgBootException("未知异常");
            }
        }catch (Exception e){
//            for(HyShippingNoteBill shippingNoteBillDb :  shippingNoteBills){
//                shippingNoteBillDb.setUploadStatus("2");
//                shippingNoteBillDb.setUploadDate(new Date());
//                shippingNoteBillDb.setReportFailureReason(e.getMessage());
//                shippingNoteBillDb.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode());
//            }
//            hyShippingNoteBillService.updateBatchById(shippingNoteBills);

            hyShippingNote.setTongdaUploadStatus("2");
            hyShippingNote.setTongdaUploadDate(new Date());
            hyShippingNote.setAlctStatus(e.getMessage());
            hyShippingNoteService.updateById(hyShippingNote);
        }
        return Result.error("上报资金流水失败");
    }

    /**
     * 淮北市上报电子运单
     *
     * @param hyShippingNote
     * @return
     */
    public Result uploadHuaibeiShippingNote(HyShippingNote hyShippingNote) {
        HyShippingNote ShippingNoteDb = hyShippingNoteService.getById(hyShippingNote.getId());
        if (ShippingNoteDb == null) {
            throw new JeecgBootException("未找到对应数据");
        }
        if ("1".equals(ShippingNoteDb.getDaodaUploadStatus())) {
            return Result.OK("该运单已上报");
        }

//        if (!"2".equals(hyShippingNote.getUploadStatus())) {
//            baseReportService.checkShippingNoteRoute(hyShippingNote);
//        }

        if (StringUtils.isBlank(hyShippingNote.getShippingContract())) {
            throw new JeecgBootException(hyShippingNote.getShippingNoteNumber() + "该运单运输合同信息不能为空");
        }

        String overload = HyCfgUtil.getCfgItem2("huaibei_report","overload");
        if (StrUtil.isEmpty(overload) && "1".equals(overload)) {
            HyVehicle hyVehicle = this.vehicleService.getById(ShippingNoteDb.getVehicleId());
            if (ShippingNoteDb.getGoodsWeightLoading().compareTo(hyVehicle.getVehicleTonnage()) > 0 ) {
                log.error("淮北监管上报超载 货物重量 {} > 核定载质量 {}", ShippingNoteDb.getGoodsWeightLoading(), hyVehicle.getVehicleTonnage());
                return Result.error("货物重量超过车辆核定载质量，货物重量:" + ShippingNoteDb.getGoodsWeightLoading() + " ,车辆核定载质量:" + hyVehicle.getVehicleTonnage());
            }
        }

        ShippingNoteDTO shippingNoteDTO = baseReportService.shippingNoteTOShippingNoteDTO(ShippingNoteDb, hyShippingNote.getCompanyId());
        String jsonString = JSONObject.toJSONString(shippingNoteDTO);

        String reporDocType = ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_YD.getCode();
        ReportDocTypeEnum reportDocTypeEnum = ReportDocTypeEnum.getReportDocTypeEnumByVal(reporDocType);

        try {
            String reportResult = huaiBeiReportPlatformService.sendReport(reportDocTypeEnum, jsonString, false);
            if (StringUtils.isNotEmpty(reportResult)) {
                R result = JSON.parseObject(reportResult, R.class);
                boolean res = result.getCode() == 1001 && result.isSuccess() ? true : false;
                if (res) { //上传合同
                    String shippingContract = uploadpath + File.separator + hyShippingNote.getShippingContract();
                    this.uploadFile(
                            new FileDTO()
                                    .setCategory(FileCodeEnum.FILE_CODE_TYPE_31.type)
                                    .setShippingNoteNumber(hyShippingNote.getShippingNoteNumber())
                                    .setImageId(FileCodeEnum.FILE_CODE_TYPE_31.code)
                                    .setImageType("pdf")
                                    .setBase64Image(PdfEditUtil.fileToBase64(shippingContract))
                    );

                    //如果上报成功，接着上报位置信息
                    this.uploadLocation(hyShippingNote);
                }
                //占用道达上报字段
                ShippingNoteDb.setDaodaUploadStatus(res ? "1" : "2");
                ShippingNoteDb.setDaodaUploadTime(new Date());
                ShippingNoteDb.setDaodaFailureReason(res ? "" : result.getMsg() + result.getData());
//                ShippingNoteDb.setReportFailureReason(res ? "" : result.getMsg() + result.getData());
//                ShippingNoteDb.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode());
                hyShippingNoteService.updateById(ShippingNoteDb);

                return res ? Result.OK("上报成功") : Result.error(result.getMsg());
            } else {
                throw new JeecgBootException("未知异常");
            }
        } catch (Exception e) {
//            ShippingNoteDb.setUploadStatus("2");
//            ShippingNoteDb.setUploadDate(new Date());
//            ShippingNoteDb.setReportFailureReason(e.getMessage());
//            ShippingNoteDb.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode());
//            hyShippingNoteService.updateById(ShippingNoteDb);
            ShippingNoteDb.setDaodaUploadStatus("2");
            ShippingNoteDb.setDaodaUploadTime(new Date());
            ShippingNoteDb.setDaodaFailureReason(e.getMessage());
            hyShippingNoteService.updateById(ShippingNoteDb);
        }

        return Result.error("上报运单失败");
    }


    public Result uploadVehicle(String vehicleId) {
        HyVehicle hyVehiclDb = vehicleService.getById(vehicleId);
        if (hyVehiclDb == null) {
            throw new JeecgBootException("未找到对应数据");
        }
        HyVehicleReport hyVehicleReport = new HyVehicleReport();


        if (StringUtils.isEmpty(hyVehiclDb.getRoadTransportNumber())) {
            throw new JeecgBootException("车辆道路运输证不能为空！");
        }

        if (!vehicleService.checkRTCNoV2(hyVehiclDb)) {
            throw new JeecgBootException("车辆道路运输证号不正确或者已过期，请修改后重新上传！");
        }

        HyVehicleType hyVehicleType = vehicleService.getHyVehicleType(hyVehiclDb.getVehicleType());
        if (hyVehicleType == null) {
            throw new JeecgBootException("车辆类型不存在,修改后请重新上报！");
        }
        hyVehiclDb.setVehicleType(hyVehicleType.getValue());
        VehicleDTO vehicleDTO = baseReportService.hyVehicleDTOTOVehicleDTO(hyVehiclDb);
        String jsonString = JSONObject.toJSONString(vehicleDTO);

        String reporDocType = ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_CL.getCode();
        ReportDocTypeEnum reportDocTypeEnum = ReportDocTypeEnum.getReportDocTypeEnumByVal(reporDocType);
        try {
            String reportResult = huaiBeiReportPlatformService.sendReport(reportDocTypeEnum, jsonString, false);
            if (StringUtils.isNotEmpty(reportResult)) {
                R result = JSON.parseObject(reportResult, R.class);
                boolean res = result.getCode() == 1001 && result.isSuccess() ? true : false;
                if (res) {
                    String pathTmp = uploadpath + File.separator + hyVehiclDb.getDrivingLicenseImg();
                    this.uploadFile(
                            new FileDTO()
                                    .setCategory(FileCodeEnum.FILE_CODE_TYPE_11.type)
                                    .setVehicleNumber(hyVehiclDb.getVehicleNumber())
                                    .setVehiclePlateColorCode(hyVehiclDb.getVehiclePlateColorCode())
                                    .setImageId(FileCodeEnum.FILE_CODE_TYPE_11.code)
                                    .setImageType("jpg")
                                    .setBase64Image(PdfEditUtil.fileToBase64(pathTmp))
                    );
                }

                //淮北监管固定公司ID，没什么意义
                hyVehicleReport.setVehicleId(vehicleId);
                hyVehicleReport.setCompanyId("1000");
                hyVehicleReport.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode());
                hyVehicleReport.setVehicleUploadStatus(res ? "1" : "2");
                hyVehicleReport.setUploadDate(new Date());
                hyVehicleReport.setVehicleReportFailureReason(result.getMsg() + result.getData());
                vehicleReportService.saveVehicleReport(hyVehicleReport);

                this.vehicleService.updateJgUploadStatusById(vehicleId, "1");
                return res ? Result.OK("上报成功") : Result.error(result.getMsg());
            } else {
                throw new JeecgBootException("未知异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("上报车辆失败,{}", e.getMessage());
        }

        return Result.error("上报车辆失败");
    }


    public Result uploadDriver(String driverId) {
        HyDriver hyDriverDb = driverService.getById(driverId);
        if (hyDriverDb == null) {
            throw new JeecgBootException("未找到对应数据");
        }
        HyDriverReport hyDriverReport = new HyDriverReport();
        try {
            //人员从业资格证验证
//            if(!driverService.checkQualificationV2(hyDriverDb)){
//                throw new JeecgBootException("人员从业资格证验证号不正确或者已过期，请修改后重新上传！");
//            }

            DriverDTO driverDTO = baseReportService.hyDriverTODriverDTO(hyDriverDb);
            String jsonString = JSONObject.toJSONString(driverDTO);

            String reporDocType = ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_JSY.getCode();
            ReportDocTypeEnum reportDocTypeEnum = ReportDocTypeEnum.getReportDocTypeEnumByVal(reporDocType);
            String reportResult = huaiBeiReportPlatformService.sendReport(reportDocTypeEnum, jsonString, false);
            if (StringUtils.isNotEmpty(reportResult)) {
                R result = JSON.parseObject(reportResult, R.class);
                boolean  res = result.getCode() == 1001 && result.isSuccess() ? true : false;
                if(res){
                    String driverImg =  uploadpath  + File.separator + hyDriverDb.getDriverImg() ;
                    String idcardImg =  uploadpath  + File.separator + hyDriverDb.getIdcardImg() ;
                    String idcardImgBack =  uploadpath  + File.separator + hyDriverDb.getIdcardImgBack() ;
                    List<FileDTO> fileDTOS = this.getFileDTOS(hyDriverDb, driverImg, idcardImg, idcardImgBack);
                    for(FileDTO fileDTO : fileDTOS){
                        this.uploadFile(fileDTO);
                    }
                }

                hyDriverReport.setDriverId(driverId);
                //淮北监管固定公司ID，没什么意义
                hyDriverReport.setCompanyId("1000");
                hyDriverReport.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode());
                hyDriverReport.setDriverUploadStatus(res ? "1" : "2");
                hyDriverReport.setUploadDate( new Date() );
                hyDriverReport.setDriverReportFailureReason(result.getMsg()+result.getData() );
                hyDriverReportService.saveDriverReport(hyDriverReport);

                this.driverService.updateJgUploadStatusById(driverId, "1");
                return res ? Result.OK() : Result.error(result.getMsg());
            }else{
                throw new JeecgBootException("未知异常");
            }
        }catch (Exception e){
           e.printStackTrace();
           log.error("上报司机失败, {}", e.getMessage());
        }

        return Result.error("上报司机失败");
    }

    /**
     * 组装司机附件信息
     * @param hyDriverDb
     * @param driverImg
     * @param idcardImg
     * @param idcardImgBack
     * @return
     */
    private List<FileDTO> getFileDTOS(HyDriver hyDriverDb, String driverImg, String idcardImg, String idcardImgBack) {
        List<FileDTO> fileDTOS =  new ArrayList<>();
        fileDTOS.add(new FileDTO()
                .setCategory(FileCodeEnum.FILE_CODE_TYPE_21.type)
                .setDrivingLicense(hyDriverDb.getDrivingLicense())
                .setImageId(FileCodeEnum.FILE_CODE_TYPE_21.code)
                .setImageType("jpg")
                .setBase64Image(  PdfEditUtil.fileToBase64(driverImg) )
        );
        fileDTOS.add(new FileDTO()
                .setCategory(FileCodeEnum.FILE_CODE_TYPE_22.type)
                .setDrivingLicense(hyDriverDb.getDrivingLicense())
                .setImageId(FileCodeEnum.FILE_CODE_TYPE_22.code)
                .setImageType("jpg")
                .setBase64Image( PdfEditUtil.fileToBase64(idcardImg) )
        );
        fileDTOS.add(new FileDTO()
                .setCategory(FileCodeEnum.FILE_CODE_TYPE_23.type)
                .setDrivingLicense(hyDriverDb.getDrivingLicense())
                .setImageId(FileCodeEnum.FILE_CODE_TYPE_23.code)
                .setImageType("jpg")
                .setBase64Image( PdfEditUtil.fileToBase64(idcardImgBack) )
        );
        return fileDTOS;
    }

    /**
     * 文件上传
     * @param fileDTO
     */
    private void uploadFile(FileDTO fileDTO) {

        String reporDocType = ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_FJ1001.getCode();
        ReportDocTypeEnum reportDocTypeEnum = ReportDocTypeEnum.getReportDocTypeEnumByVal(reporDocType);
        try {
            String jsonSendString = JSONObject.toJSONString(fileDTO);
            log.info("淮北监管发送文件请求 -> {} {}", fileDTO.getCategory(), fileDTO.getImageId());
            this.huaiBeiReportPlatformService.sendReport(reportDocTypeEnum, jsonSendString, true);
        } catch (Exception e) {
            log.error("uploadFile Exception:{}" ,  e.getMessage());
        }

    }

    /**
     * 驾驶员位置信息
     * 通过此接口，在运单起运和确认送达时，经驾驶员授权同意后，实时采集和上传驾驶员地理位置信息。
     * @param hyShippingNote
     */
    private void uploadLocation(HyShippingNote hyShippingNote) {

        String reporDocType = ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_WZCX1.getCode();
        ReportDocTypeEnum reportDocTypeEnum = ReportDocTypeEnum.getReportDocTypeEnumByVal(reporDocType);

        try {
            double[] sendLngLon = LngLonUtil.gcj02_To_gps84_new(hyShippingNote.getSendLatitude().doubleValue() ,hyShippingNote.getSendLongitude().doubleValue());
            double[] receivedLngLon = LngLonUtil.gcj02_To_gps84_new(hyShippingNote.getReceiveLatitude().doubleValue() ,hyShippingNote.getReceiveLongitude().doubleValue());

            LocationDTO sendLocationDTO = new LocationDTO();
            sendLocationDTO.setSendToProDateTime(
                    DateUtils.dateToStringDateTime(new Date())
            );
            sendLocationDTO.setShippingNoteNumber(hyShippingNote.getShippingNoteNumber());

            sendLocationDTO.setLocationType(0);
            sendLocationDTO.setSendDateTime(
                    DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtLoading()) );
            sendLocationDTO.setLatitude(new BigDecimal(sendLngLon[0] * 600000).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            sendLocationDTO.setLongitude(new BigDecimal(sendLngLon[1] * 600000).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            sendLocationDTO.setCountrySubdivisionCode(hyShippingNote.getSendAreaCode());
            sendLocationDTO.setLocationText(hyShippingNote.getSendAddress());
            String jsonSendString = JSONObject.toJSONString(sendLocationDTO);
            String reportResult = huaiBeiReportPlatformService.sendReport(reportDocTypeEnum, jsonSendString, false);

            LocationDTO receiveLocationDTO = new LocationDTO();
            receiveLocationDTO.setSendToProDateTime(
                    DateUtils.dateToStringDateTime(new Date())
            );
            receiveLocationDTO.setShippingNoteNumber(hyShippingNote.getShippingNoteNumber());

            receiveLocationDTO.setLocationType(1);
            receiveLocationDTO.setSendDateTime(
                    DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtUnloading()) );
            receiveLocationDTO.setLatitude(new BigDecimal(receivedLngLon[0] * 600000).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            receiveLocationDTO.setLongitude(new BigDecimal(receivedLngLon[1] * 600000).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            receiveLocationDTO.setCountrySubdivisionCode(hyShippingNote.getReceiveAreaCode());
            receiveLocationDTO.setLocationText(hyShippingNote.getReceiveAddress());
            String jsonReceiveString = JSONObject.toJSONString(receiveLocationDTO);
            String reportResult1 = huaiBeiReportPlatformService.sendReport(reportDocTypeEnum, jsonReceiveString, false);

        } catch (Exception e) {
            log.error("淮北监管上报位置信息:{}" ,  e.getMessage());
        }

    }

}
